package com.whd.controller;

import com.github.pagehelper.PageInfo;
import com.whd.domain.ResultVo;
import com.whd.entity.Dept;
import com.whd.entity.Emp;
import com.whd.service.DeptService;
import com.whd.service.EmpService;
import com.whd.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author whd
 * @date 2023/07/01 11:03:40
 */
@RestController
@RequestMapping("emp")
public class EmpController {

    @Autowired
    private EmpService empService;
    @Autowired
    private FileService fileService;
    @Autowired
    private DeptService deptService;
    @PostMapping
    public ResultVo save(Emp emp, MultipartFile photoFile, HttpServletRequest request) throws IOException {
        String path = request.getRealPath("imgs");
        String newFile = fileService.addPhoto(path, photoFile);
        emp.setPhoto(newFile);
        int rows = empService.save(emp);
        return rows == 1 ? ResultVo.success("添加成功", emp) : ResultVo.fail("添加失败");
    }

    @GetMapping("{pageNum}/{pageSize}")
    public ResultVo findALl(@PathVariable Integer pageNum, @PathVariable Integer pageSize, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        session.setAttribute("pageNum", pageNum);
        session.setAttribute("pageSize", pageSize);
        List<Dept> deptList = deptService.findAll();
        PageInfo<Emp> pageInfo = empService.findALl(pageNum, pageSize);
        map.put("deptList", deptList);
        map.put("pageInfo", pageInfo);
        return ResultVo.success(map);
    }

    @DeleteMapping("{empid}")
    public ResultVo delete(@PathVariable Integer empid) {
        int rows = empService.delete(empid);
        return rows == 1 ? ResultVo.success("删除成功") : ResultVo.fail("删除失败");
    }

    @PutMapping
    public ResultVo update(Emp emp ,MultipartFile photoFile, HttpServletRequest request ) throws IOException {
        int rows;

        if (photoFile == null) {
            rows = empService.update(emp);
        }else {
            String path = request.getRealPath("imgs");
            String newFile = fileService.addPhoto(path, photoFile);
            String oldFile = emp.getPhoto();
            emp.setPhoto(newFile);
            rows = empService.update(emp);
            if (rows > 0) {
                fileService.removePhoto(path, oldFile);
            }
        }
        return rows > 0 ? ResultVo.success("修改成功") : ResultVo.fail("修改失败");
    }

    @PostMapping("other")
    public ResultVo findByOther(@RequestParam Map<String, Object> map, HttpSession session) throws ParseException {

        Object pageNum = session.getAttribute("pageNum")==null?1:session.getAttribute("pageNum");
        Object pageSize = session.getAttribute("pageSize")==null?5:session.getAttribute("pageSize");

        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        System.out.println(map+"=======================================");
        PageInfo<Emp> pageInfo = empService.findByOther(map);
        return ResultVo.success(pageInfo);
    }

}
